package com.cxm.weilai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cxm.weilai.dao.UserMapper;
import com.cxm.weilai.entity.User;
import com.cxm.weilai.service.ILoginService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: weilai-cloud
 * @description: 登录接口实现类
 * @author: ChenMan
 * @create: 2019-03-28 13:49
 */
@Service("loginService")
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private UserMapper userMapper;

    //查询用户通过用户名
    @Override
    public User findByName(Long id) {
//    return userRepository.findByName(name);
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public JSONObject regist(User user) {
        // 生成盐（部分，需要存入数据库中）
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
        // 将原始密码加盐（上面生成的盐），并且用md5算法加密三次，将最后结果存入数据库中
        String newpwd = new Md5Hash(user.getPassword(), salt, 2).toString();
        user.setPassword(newpwd);
        user.setSalt(salt);
        userMapper.insertSelective(user);
        return null;
    }
}
